<?php
// ------------------------------------------------------------------------------------
// Bestand:       Feeds
// Versie:        1.2
// Projekt:       Cursus MasterClass van Eduvision
// Datum:         24-06-2008
// Auteur(s):     Ronald Obbens
// Email(s):      ronaldobbens@gmail.com
// Revisie(s):	  1.1 -> extends controller
//				  1.2 -> show feed renders an dom document RSS feed
//
// Beschrijving:  Controller that controls everything to do with the generation of the rss feeds	
// ------------------------------------------------------------------------------------
//


require_once ('Controller.php');
require_once ('newsController.php');
require_once ('questionController.php');

class feedsController extends Controller {
	/**
	 * index action
	 *
	 * default action off the controller, shows a list of feeds
	 */
	public function index() {
		parent::render ( 'feedsindex.tpl' );
	}
	
	/**
	 * showFeed action
	 * 
	 * shows the defined feed as XML rss feed
	 *
	 * @param string $p_sName
	 */
	public function showFeed($p_sName) {
	
		//generate root
		header ( 'Content-type: text/xml' );
		$oDomDocument = new DOMDocument ( );
		$oRoot = $oDomDocument->createElement ( 'rss' );
		$oRoot->setAttribute ( 'version', '2.0' );
		$oDomDocument->appendChild( $oRoot );
		
		switch ($p_sName) {
			case 'questions' :
				
				//set the channel informatieon	
				$oChannel = $oDomDocument->createElement ( 'channel' );
				$oRoot->appendChild($oChannel);
				
				$oTitle = $oDomDocument->createElement ( 'title', 'Askkid CF - Vragen Feed' );
				$oLink = $oDomDocument->createElement ( 'link', 'http://www.askkid.nl' );
				$oDesc = $oDomDocument->createElement ( 'description', 'Vragen over Taaislijmziekte/Cystic Fibrosis, stel ze hier!' );
				$oLang = $oDomDocument->createElement ( 'language', 'nl' );
				
				$oChannel->appendChild ( $oTitle );
				$oChannel->appendChild ( $oLink );
				$oChannel->appendChild ( $oDesc );
				$oChannel->appendChild ( $oLang );
				
				//set the questions
				$questions = new questionController ( );
								
				foreach ( $questions->getLastQuestions ( 10 ) as $question ) {
					$oQuestion = $oDomDocument->createElement ( 'item' );
					
					$oQuestionTitle = $oDomDocument->createElement ( 'title', $question->getQuestion () );
					$oQuestionLink = $oDomDocument->createElement ( 'link', DOMAIN_URL . '/question/showitem/' . $question->getId () . '/' . $question->getQuestion ());
					$oQuestionDescription = $oDomDocument->createElement ( 'description', $question->getAnswer () );
					
					$oQuestion->appendChild ( $oQuestionTitle );
					$oQuestion->appendChild ( $oQuestionLink );
					$oQuestion->appendChild ( $oQuestionDescription );
					
					$oChannel->appendChild ( $oQuestion );
				}
				
				print $oDomDocument->saveHTML();				
				break;
			
			case 'news' :
				
				//set the channel informatieon	
				$oChannel = $oDomDocument->createElement ( 'channel' );
				$oRoot->appendChild ( $oChannel );
				
				$oTitle = $oDomDocument->createElement ( 'title', 'Askkid CF - Nieuws Feed' );
				$oLink = $oDomDocument->createElement ( 'link', 'http://www.askkid.nl' );
				$oDesc = $oDomDocument->createElement ( 'description', 'Vragen over Taaislijmziekte/Cystic Fibrosis, stel ze hier!' );
				$oLang = $oDomDocument->createElement ( 'language', 'nl' );
				
				$oChannel->appendChild ( $oTitle );
				$oChannel->appendChild ( $oLink );
				$oChannel->appendChild ( $oDesc );
				$oChannel->appendChild ( $oLang );
				
				//set the questions
				$newsItems = new newsController ( );
				$newsItems->getLastNewsItems ( 10 );
				
				foreach ( $newsItems->getLastNewsItems ( 10 ) as $newsItem ) {
					$oNewsItem = $oDomDocument->createElement ( 'item' );
					
					$oNewsItemTitle = $oDomDocument->createElement ( 'title', $newsItem->getTitle() );
					$oNewsItemLink = $oDomDocument->createElement ( 'link', DOMAIN_URL . '/news/showitem/' . $newsItem->getId () . '/' . $newsItem->getTitle () );
					$oNewsItemDescription = $oDomDocument->createElement ( 'description', $newsItem->getContent() );
					
					$oNewsItem->appendChild ( $oNewsItemTitle );
					$oNewsItem->appendChild ( $oNewsItemLink );
					$oNewsItem->appendChild ( $oNewsItemDescription );
					
					$oChannel->appendChild ( $oNewsItem );
				}
			
				print $oDomDocument->saveXML ();
				break;
		}
	}
}
?>